﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Config;
using DTO;
using AutoMapper;

namespace DataAccess
{
    public static class AccountTypeDAO
    {
        private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static AccountTypeDAO()
        {
            XmlConfigurator.Configure();          
        }

        public static List<AccountTypeDTO> GetAccountTypeList()
        {
            try
            {
                List<AccountTypeDTO> lstAccountType = new List<AccountTypeDTO>();

                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = from AccountType in context.ACCOUNT_TYPE_LIST 
                            select AccountType;
                foreach (var item in query)
                {
                    AccountTypeDTO dtoAccountType = new AccountTypeDTO();
                    dtoAccountType = Mapper.Map<ACCOUNT_TYPE , AccountTypeDTO>(item);
                    lstAccountType.Add(dtoAccountType);
                }
                return lstAccountType;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }

        public static AccountTypeDTO  FindAccountType(AccountTypeDTO dtoAccountType, string searchOption)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

                IQueryable<ACCOUNT_TYPE> query=null;
                if (searchOption == AccountTypeColumns.ACCOUNT_TYPE_ID)
                {
                    query = from AccountType in context.ACCOUNT_TYPE_LIST
                            where AccountType.ACCOUNT_TYPE_ID == dtoAccountType.ACCOUNT_TYPE_ID
                            select AccountType;
                }
                else if (searchOption == AccountTypeColumns.ACCOUNT_TYPE_NAME)
                {
                    query = from AccountType in context.ACCOUNT_TYPE_LIST
                            where AccountType.ACCOUNT_TYPE_NAME == dtoAccountType.ACCOUNT_TYPE_NAME
                            select AccountType;
                }

                if (query == null)
                {
                    logger.Debug("Chưa hỗ trợ tìm kiếm theo " + searchOption);
                    return null;
                }

                if (query.Count() == 0)
                    return null;
                AccountTypeDTO Result = new AccountTypeDTO();
                ACCOUNT_TYPE acc = query.First();                
                Result = Mapper.Map<ACCOUNT_TYPE, AccountTypeDTO >(acc);
                return Result;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return null;
            }
        }    

        public static bool AddNewAccountType(AccountTypeDTO dtoAccountType)
        {
            try
            {
                ACCOUNT_TYPE obj = new ACCOUNT_TYPE();
                //Mapping                
                obj = Mapper.Map<AccountTypeDTO, ACCOUNT_TYPE>(dtoAccountType);
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                context.AddToACCOUNT_TYPE_LIST(obj);
                context.SaveChanges(true);
                dtoAccountType.ACCOUNT_TYPE_ID = obj.ACCOUNT_TYPE_ID;
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool UpdateAccountType(AccountTypeDTO dtoAccountType)
        {
            QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();

            ACCOUNT_TYPE AccountType = context.ACCOUNT_TYPE_LIST.First(g => g.ACCOUNT_TYPE_ID == dtoAccountType.ACCOUNT_TYPE_ID);
            
            AccountType.ACCOUNT_TYPE_NAME = dtoAccountType.ACCOUNT_TYPE_NAME;
            AccountType.ALLOW_ACCOUNT_MANAGEMENT = dtoAccountType.ALLOW_ACCOUNT_MANAGEMENT;
            AccountType.ALLOW_CHANGE_PARAMENTER  = dtoAccountType.ALLOW_CHANGE_PARAMENTER ;
            AccountType.ALLOW_CHANGE_RANKING_PRIORITY  = dtoAccountType.ALLOW_CHANGE_RANKING_PRIORITY ;
            AccountType.ALLOW_CREATE_LEAGUE_REPORT  = dtoAccountType.ALLOW_CREATE_LEAGUE_REPORT ;
            AccountType.ALLOW_CREATE_MATCH_SCHEDULE  = dtoAccountType.ALLOW_CREATE_MATCH_SCHEDULE ;
            AccountType.ALLOW_MATCH_RESULT_MANAGEMENT  = dtoAccountType.ALLOW_MATCH_RESULT_MANAGEMENT ;
            AccountType.ALLOW_PLAYER_INFORMATION_MANAGEMENT  = dtoAccountType.ALLOW_PLAYER_INFORMATION_MANAGEMENT ;
            AccountType.ALLOW_PLAYER_INFORMATION_SEARCHING  = dtoAccountType.ALLOW_PLAYER_INFORMATION_SEARCHING ;
            AccountType.ALLOW_TEAM_REGISTRATION  = dtoAccountType.ALLOW_TEAM_REGISTRATION;            
            
            context.SaveChanges(true);
            return true;
        }

        public static bool DeleteAAccountType(AccountTypeDTO dtoAccountType)
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                ACCOUNT_TYPE AccountType = context.ACCOUNT_TYPE_LIST.First(g => g.ACCOUNT_TYPE_ID  == dtoAccountType.ACCOUNT_TYPE_ID );
                context.DeleteObject(AccountType);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }

        public static bool DeleteAllAccountType()
        {
            try
            {
                QUANLYBONGDA_VDQG_1042102Entities context = new QUANLYBONGDA_VDQG_1042102Entities();
                var query = (from AccountType in context.ACCOUNT_TYPE_LIST 
                             select AccountType);
                foreach (var item in query)
                {
                    context.DeleteObject(item);
                }
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return false;
            }
        }
    }
}
